package com.platform.qujia.controller;

import com.platform.qujia.anno.SimpleRepeatedReqLimit;
import com.platform.qujia.enums.PayTypeEnum;
import com.platform.qujia.param.request.PayOrderRequest;
import com.platform.qujia.pojo.vo.PayOrderVO;
import com.platform.qujia.pojo.vo.StoredCardVO;
import com.platform.qujia.service.AppUserOrderService;
import com.platform.qujia.utils.CodeMsg;
import com.platform.qujia.utils.ResultData;
import com.platform.qujia.utils.exception.CustomException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 会员列表消费扣减controller
 * @date 2023/10/16
 */
@RestController
@CrossOrigin
@RequestMapping("appUser/order")
@Api(tags = "消费扣款")
public class AppUserOrderController {

    private static final Logger log = LoggerFactory.getLogger(AppUserOrderController.class);

    @Autowired
    private AppUserOrderService appUserOrderService;
    private Pattern decimalPattern = Pattern.compile("^(0|[1-9][0-9]*)(\\.[0-9]{1,2})?$");

    @PostMapping("create")
    @ApiOperation("创建订单")
    @SimpleRepeatedReqLimit(name = "appUserOrderCreate:",express = "[0].getConsumerId()", condition = "[0].getConsumerId() != null",expire = 25)
    public ResultData<PayOrderVO> createOrder(@RequestBody PayOrderRequest request) throws Exception {
        if (request.getEnableDiscount() == null) {
            request.setEnableDiscount(Boolean.TRUE);
        }
        if (request.getEnableCoupon() == null) {
            request.setEnableCoupon(Boolean.TRUE);
        }
        checkParam(request);
        if (StringUtils.equals("tyk",request.getPayType())) {
            request.setPayType(PayTypeEnum.dzk.getType());
            request.setGenericPay(Boolean.TRUE);
        }
        return ResultData.success(appUserOrderService.createOrder(request));
    }

    @PostMapping("update")
    @ApiOperation("更新订单")
    @SimpleRepeatedReqLimit(name = "appUserOrderStatus:",express = "[0].getOrderNo()",
            condition = "[0].getOrderNo() != null && [0].getOrderNo().length() > 0",expire = 25)
    public ResultData<PayOrderVO> updateOrder(@RequestBody PayOrderRequest request) throws Exception {
        if (request.getEnableDiscount() == null) {
            request.setEnableDiscount(Boolean.TRUE);
        }
        if (request.getEnableCoupon() == null) {
            request.setEnableCoupon(Boolean.TRUE);
        }
        if (StringUtils.isBlank(request.getOrderNo())) {
            return ResultData.error(CodeMsg.NO_ORDER);
        }
        checkParam(request);
        if (StringUtils.equals("tyk",request.getPayType())) {
            request.setPayType(PayTypeEnum.dzk.getType());
            request.setGenericPay(Boolean.TRUE);
        }
        return ResultData.success(appUserOrderService.updateOrder(request));
    }


    @PostMapping("pay")
    @ApiOperation("支付")
    @SimpleRepeatedReqLimit(name = "appUserOrderStatus:",express = "[0].getOrderNo()",
            condition = "[0].getOrderNo() != null && [0].getOrderNo().length() > 0",expire = 25)
    public ResultData pay(@RequestBody PayOrderRequest request){
        if (StringUtils.isBlank(request.getOrderNo())) {
            return ResultData.error(CodeMsg.NO_ORDER);
        }
        if (request.getStationId() == null) {
            throw new CustomException(CodeMsg.MISSING_PARAM);
        }
        if (request.getConsumerId()==null) {
            throw new CustomException(CodeMsg.MISSING_PARAM);
        }
        appUserOrderService.pay(request);
        return ResultData.success("支付成功");
    }

    @PostMapping("cardList")
    @ApiOperation("储值卡列表")
    public ResultData<List<StoredCardVO>> queryStoredCardVOList(@RequestBody PayOrderRequest request){
        return ResultData.success(appUserOrderService.queryStoredCardVOList(request));
    }

    private void checkParam(PayOrderRequest request){
        if (request.getStationId() == null) {
            throw new CustomException(CodeMsg.MISSING_PARAM);
        }
        if (StringUtils.isBlank(request.getOilPersonnel())) {
            throw new CustomException(CodeMsg.MISSING_PARAM);
        }
        if (StringUtils.isBlank(request.getGunName())) {
            throw new CustomException(CodeMsg.MISSING_PARAM);
        }
        if (StringUtils.isBlank(request.getReceivableAmt())) {
            throw new CustomException(CodeMsg.MISSING_PARAM);
        }
        if (request.getConsumerId()==null) {
            throw new CustomException(CodeMsg.MISSING_PARAM);
        }
        Matcher matcher = decimalPattern.matcher(request.getReceivableAmt());
        if (!matcher.matches()) {
            throw new CustomException("金额格式不正确",CodeMsg.MISSING_PARAM.getRetCode());
        }
    }


}
